#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=10010;
int f[N];
struct node{
    int s,e,l;
}stone[N];

bool cmp(node a,node b)
{
    return a.s*b.l<b.s*a.l;
}
int main()
{
    int t;
    cin>>t;
    int num=0;
    while(t--)
    {
        memset(f,0,sizeof(f));
        int n;
        int m=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>stone[i].s>>stone[i].e>>stone[i].l;
            m+=stone[i].s;
        }
        sort(stone+1,stone+n+1,cmp);
        for(int i=1;i<=n;i++)
        {
            int s=stone[i].s, e=stone[i].e, l=stone[i].l;
            for(int j=m;j>=s;j--)
            {
                f[j]=max(f[j],f[j-s]+e-(j-s)*l);
            }
        }
        int ans=0;
        for(int i=0;i<=m;i++) ans=max(ans,f[i]);
        printf("Case #%d: %d\n",++num,ans);
    }

    return 0;
}
